package com.xtianzhuang.www.study2019.practice.leetcode.study.array;

import java.util.Arrays;

/**
 * 寻找数组的中心索引
 * @author xtian.zhuang
 * @date Jul 31, 2019
 */
class Solution {
	public static void main(String[] args) {
		int index = pivotIndex(new int[]{1, 7, 3, 6, 5, 6});
		// System.out.println(index);s
		System.out.println(Integer.MAX_VALUE);
		int index2 = dominantIndex(new int[]{1, 0});
		// System.out.println(index2);

		plusOneNew(new int[]{4, 3, 2, 1});
		plusOneNew(new int[]{1, 9, 9});
		plusOneNew(new int[]{9, 9, 9});
		plusOneNew(new int[]{9});
	}

	public static int pivotIndex(int[] nums) {
		int total = 0;
		int sum = 0;
		for (int i = 0; i < nums.length; i++) {
			total += nums[i];
		}
		for (int i = 0; i < nums.length; i++) {
			sum += nums[i];
			if ((sum - nums[i]) * 2 == total - nums[i]) {
				return i;
			}
		}
		return -1;

	}

	public static int dominantIndex(int[] nums) {
		int[] numsNew = Arrays.copyOf(nums, nums.length);
		Arrays.sort(nums);
		if (nums.length >= 2) {
			if (nums[nums.length - 1] >= 2 * nums[nums.length - 2]) {
				int val = nums[nums.length - 1];
				for (int i = 0; i < numsNew.length; i++) {
					if (val == numsNew[i]) {
						return i;
					}
				}
			} else {
				return -1;
			}
		} else {
			return 0;
		}
		return -1;
	}

	/**
	 * 如果大于Integer.max 有问题
	 * @param digits
	 * @return
	 */
	public static int[] plusOne(int[] digits) {
		String numStr = "";
		for (int i = 0; i < digits.length; i++) {
			numStr += digits[i];
		}
		Integer num = Integer.valueOf(numStr) + 1;
		int[] newStr = new int[String.valueOf(num).length()];
		for (int i = newStr.length - 1; i >= 0; i--) {
			newStr[i] = num % 10;
			num = num / 10;
		}
		// System.out.println(Arrays.toString(newStr));
		return newStr;
	}

	public static int[] plusOneNew(int[] digits) {
		if (digits.length == 1) {
			if (digits[0] == 9) {
				return new int[]{1, 0};
			} else {
				digits[0] = digits[0] + 1;
				return digits;
			}
		}
		for (int i = digits.length - 1; i > 0; i--) {
			if (i == digits.length - 1) {
				if (digits[digits.length - 1] == 9) {
					digits[digits.length - 1] = 0;
					digits[digits.length - 2] = digits[digits.length - 2] + 1;
				} else {
					digits[digits.length - 1] = digits[digits.length - 1] + 1;
				}
			}
			if (i != 0) {
				if (digits[i] == 10) {
					digits[i - 1] = digits[i - 1] + 1;
					digits[i] = 0;
				}
			}
		}
		if (digits[0] == 10) {
			int[] digitsNew = new int[digits.length + 1];
			digitsNew[0] = 1;
			digitsNew[1] = 0;
			for (int i = 1; i < digits.length; i++) {
				digitsNew[i + 1] = digits[i];
			}
			// System.out.println(Arrays.toString(digitsNew));
			return digitsNew;
		}
		// System.out.println(Arrays.toString(digits));
		return digits;
	}

	/**
	 * 两数之和
	 * 给定一个整数数组 nums 和一个目标值 target，请你在该数组中找出和为目标值的那 两个 整数，并返回他们的数组下标。
	 * @param nums
	 * @param target
	 * @return
	 */
	public static int[] twoSum(int[] nums, int target) {
		for (int i = 0; i < nums.length - 1; i++) {
			for (int j = i + 1; j < nums.length; j++) {
				if (nums[i] + nums[j] == target) {
					return new int[]{i, j};
				}
			}
		}
		return new int[]{};
	}

	/**
	 * 大佬写的
	 * @param nums
	 * @param target
	 * @return
	 */
	public int[] twoSum2(int[] nums, int target) {
		int indexArrayMax = 2047;
		int[] indexArrays = new int[indexArrayMax + 1];
		int diff = 0;
		for (int i = 0; i < nums.length; i++) {
			// 获取另一个值
			diff = target - nums[i];
			if (indexArrays[diff & indexArrayMax] != 0) {
				return new int[]{indexArrays[diff & indexArrayMax] - 1, i};
			}
			indexArrays[nums[i] & indexArrayMax] = i + 1;
		}
		throw new IllegalArgumentException("No two sum value");


	}

}